package models

import (
	"time"

	"gorm.io/gorm"
)

// Agent 智能代理模型
type Agent struct {
	ID          uint           `json:"id" gorm:"primarykey"`
	Name        string         `json:"name" gorm:"not null;size:100"`
	Description string         `json:"description" gorm:"size:500"`
	Type        string         `json:"type" gorm:"not null;size:50"`   // conversational, task-based, autonomous
	Status      string         `json:"status" gorm:"default:inactive"` // active, inactive, training, error
	Config      string         `json:"config" gorm:"type:jsonb"`       // JSON配置
	Model       string         `json:"model" gorm:"not null;size:100"` // AI模型名称
	Version     string         `json:"version" gorm:"default:1.0.0"`
	CreatedBy   uint           `json:"created_by"`
	UpdatedBy   uint           `json:"updated_by"`
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`

	// 关联
	Tasks      []Task      `json:"tasks,omitempty" gorm:"foreignKey:AgentID"`
	Executions []Execution `json:"executions,omitempty" gorm:"foreignKey:AgentID"`
}

// Task 任务模型
type Task struct {
	ID          uint           `json:"id" gorm:"primarykey"`
	AgentID     uint           `json:"agent_id" gorm:"not null"`
	Name        string         `json:"name" gorm:"not null;size:100"`
	Description string         `json:"description" gorm:"size:1000"`
	Type        string         `json:"type" gorm:"not null;size:50"`  // single, recurring, conditional
	Priority    int            `json:"priority" gorm:"default:1"`     // 1-5, 5最高
	Status      string         `json:"status" gorm:"default:pending"` // pending, running, completed, failed, cancelled
	Input       string         `json:"input" gorm:"type:jsonb"`       // 输入参数
	Output      string         `json:"output" gorm:"type:jsonb"`      // 输出结果
	Config      string         `json:"config" gorm:"type:jsonb"`      // 任务配置
	ScheduledAt *time.Time     `json:"scheduled_at"`                  // 计划执行时间
	StartedAt   *time.Time     `json:"started_at"`                    // 开始执行时间
	CompletedAt *time.Time     `json:"completed_at"`                  // 完成时间
	CreatedBy   uint           `json:"created_by"`
	UpdatedBy   uint           `json:"updated_by"`
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`

	// 关联
	Agent      Agent       `json:"agent,omitempty" gorm:"foreignKey:AgentID"`
	Executions []Execution `json:"executions,omitempty" gorm:"foreignKey:TaskID"`
}

// Execution 执行记录模型
type Execution struct {
	ID          uint           `json:"id" gorm:"primarykey"`
	AgentID     uint           `json:"agent_id" gorm:"not null"`
	TaskID      *uint          `json:"task_id"`                                  // 可为空，支持直接执行
	ExecutionID string         `json:"execution_id" gorm:"uniqueIndex;not null"` // 唯一执行ID
	Status      string         `json:"status" gorm:"default:pending"`            // pending, running, completed, failed, cancelled
	Input       string         `json:"input" gorm:"type:jsonb"`
	Output      string         `json:"output" gorm:"type:jsonb"`
	Error       string         `json:"error" gorm:"type:text"`
	Logs        string         `json:"logs" gorm:"type:text"`
	Metrics     string         `json:"metrics" gorm:"type:jsonb"` // 执行指标
	Duration    int64          `json:"duration"`                  // 执行时长（毫秒）
	StartedAt   *time.Time     `json:"started_at"`
	CompletedAt *time.Time     `json:"completed_at"`
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`

	// 关联
	Agent Agent `json:"agent,omitempty" gorm:"foreignKey:AgentID"`
	Task  *Task `json:"task,omitempty" gorm:"foreignKey:TaskID"`
}

// AgentTemplate 代理模板
type AgentTemplate struct {
	ID          uint           `json:"id" gorm:"primarykey"`
	Name        string         `json:"name" gorm:"not null;size:100"`
	Description string         `json:"description" gorm:"size:500"`
	Category    string         `json:"category" gorm:"not null;size:50"`
	Template    string         `json:"template" gorm:"type:jsonb"` // 模板配置
	Tags        string         `json:"tags" gorm:"type:jsonb"`     // 标签
	IsPublic    bool           `json:"is_public" gorm:"default:false"`
	UsageCount  int            `json:"usage_count" gorm:"default:0"`
	CreatedBy   uint           `json:"created_by"`
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`
}

// TableName 指定表名
func (Agent) TableName() string {
	return "agents"
}

func (Task) TableName() string {
	return "tasks"
}

func (Execution) TableName() string {
	return "executions"
}

func (AgentTemplate) TableName() string {
	return "agent_templates"
}
